import pymysql

def check_user(username, password):
    try:
        # 连接数据库
        conn = pymysql.connect(
            host='localhost',
            user='root',  # 你的用户名
            password='010615',  # 你的密码
            database='qxyt2',  # 你的数据库名
            cursorclass=pymysql.cursors.DictCursor
        )
        if conn.open:
            print("Connected to MySQL database successfully!")

        # 创建游标对象
        with conn.cursor() as cursor:
            # 执行查询语句
            query = "SELECT * FROM users WHERE username = %s AND password = %s"
            cursor.execute(query, (username, password))

            # 获取查询结果
            result = cursor.fetchone()

            # 检查是否找到匹配的用户
            if result:
                print("User found in the database.")
                return True
            else:
                print("User not found in the database.")
                return False

    except pymysql.Error as e:
        print("Error connecting to MySQL database:", e)

    finally:
        # 关闭数据库连接
        if 'conn' in locals() and conn.open:
            conn.close()
            print("MySQL database connection closed.")
